﻿. .\ConfigurationVariables.ps1

Function ValidatePassword([string]$password)
{
    $groupCount = 0
    $minimumLength = 8

    if ($password -match "([A-Z])") { $groupCount++ }
    if ($password -match "([a-z])") { $groupCount++ }
    if ($password -match "(\d)") { $groupCount++ }
    if ($password -match "([^A-Za-z\d\s])") { $groupCount++ }

    if (($password.Length -lt $minimumLength) -or ($groupCount -lt 3))
    {
        return $false
    }
	
	return $true
}

Function ValidateHostedServiceName([string]$hostedServiceName)
{
    if ($hostedServiceName -cnotmatch "^[0-9a-zA-z]+[0-9a-zA-z-]*[0-9a-zA-z]*[0-9a-zA-z]$")
    {
        return $false
    }

    $maxLength = 63
    if ($hostedServiceName.Length -gt $maxLength)
    {
        return $false
    }

    return $true
}

Function ValidateStorageAccountName([string]$storageAccountName)
{
    $minLength = 3
    $maxLength = 24
    if ($storageAccountName -cnotmatch "^[0-9a-z]{$minLength,$maxLength}$")
    {
        return $false
    }

    return $true
}

Function ValidateContainerName([string]$containerName)
{
    $minLength = 2
    $maxLength = 62
    return ($containerName -cmatch "^(([a-z\d]((-(?=[a-z\d]))|([a-z\d])){$minLength,$maxLength})|(\`$root))$")
}

Function ValidatePath([string]$path)
{
	if (!(Test-Path $path))
	{
		Write-Error "Can't file file on path $path"
		return $false
	}
	
	return $true
}

Function ValidateDeployment()
{
	$valid = $true
	
	if (!(ValidatePassword $systemPassword))
	{
	    Write-Error @"
The system password must meet the following requirements:
Is more than eight characters in length
Does not contain all or part of the login name
Contains characters from at least three of the following categories:
    - English uppercase characters (A through Z)
    - English lowercase characters (a through z)
    - Base 10 digits (0 through 9)
    - Non-alphanumeric characters (for example: !, $, #, %)
"@

		$valid = $false
	}

    $invalidHosterServiceNameErr = @"
Hosted service name can contain only letters, numbers, and hyphens. The first and last character in the field must be a letter or number.
Hosted service name must be a string with less than 63 characters.
"@

    # Validate hpc cluster hosted service name
    if (!(ValidateHostedServiceName $deploymentName))
    {
        Write-Error "Name of hpc cluster is invalid.`n$invalidHosterServiceNameErr"
        $valid = $false
    }

    # Validate deployer hosted service name
    if (!(ValidateHostedServiceName $deployerServiceName))
    {
        Write-Error "Name of the deployer service is invalid.`n$invalidHosterServiceNameErr"
        $valid = $false
    }

    $invalidStorageAccountErr = @"
Storage account name can contain only lowercase letters and numbers. Storage account name must be between 3 and 24 characters long.
"@

    # Validate storage account name 
    if (!(ValidateStorageAccountName $deploymentName $storageMinLength $storageMaxLength))
    {
        Write-Error "Name of the storage account is invalid.`n$invalidStorageAccountErr"
        $valid = $false
    }

    $invalidContainerNameErr = @"
A container name must be a valid DNS name, conforming to the following naming rules:
    - Container names must start with a letter or number, and can contain only letters, numbers, and the dash (-) character.
    - Every dash (-) character must be immediately preceded and followed by a letter or number; consecutive dashes are not permitted in container names.
    - All letters in a container name must be lowercase.
    - Container names must be from 3 through 63 characters long.
"@
    # Validate deployment store container name
    if (!(ValidateContainerName $deploymentContainer))
    {
        Write-Error "Invalid deployment store container name.`n$invalidContainerNameErr"
        $valid = $false
    }

    # Validate install container name
    if (!(ValidateContainerName $installContainer))
    {
        Write-Error "Invalid install container name.`n$invalidContainerNameErr"
        $valid = $false
    }

    # Validate count of compute node
    if ($ComputeNodeInstanceCount -lt 1)
    {
        Write-Error "Compute node count must not be less than 1"
        $valid = $false
    }

    # Validate count of head node
    if ($HeadNodeInstanceCount -lt 1)
    {
        Write-Error "Head node count must not be less than 1"
        $valid = $false
    }

    $locations = @("Southeast Asia", "East Asia", "North Central US", "North Europe", "West Europe", "West US", "East US", "South Central US")
    if (!($locations.Contains($deployLocation)))
    {
        Write-Error "Invalid deploy location `"$deployLocation`""
        $valid = $false
    }

    # Validate paths of files
	$valid = $valid -and $(ValidatePath $deployRolePackage)
	$valid = $valid -and $(ValidatePath $clusterTemplatePackage)
	$valid = $valid -and $(ValidatePath $clusterTemplateCsCfg)
	$valid = $valid -and $(ValidatePath $clusterTemplateCsdef)
	$valid = $valid -and $(ValidatePath $startupCmd)
	$valid = $valid -and $(ValidatePath $installScript)
	$valid = $valid -and $(ValidatePath $cmdletsPath)
	
	return $valid
}